// Copyright (c) 2024，D-Robotics.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/***************************************************************************
* COPYRIGHT NOTICE
* Copyright 2019 Horizon Robotics, Inc.
* All rights reserved.
***************************************************************************/
#ifndef UTILITY_SENSOR_INC_IMX219_SETTING_H_
#define UTILITY_SENSOR_INC_IMX219_SETTING_H_

#ifdef __cplusplus
extern "C" {
#endif

#define IMX219_SHR0 (0x3050)
#define IMX219_SHR1 (0x3054)
#define IMX219_SHR2 (0x3058)
#define IMX219_SHR3 (0x305C)

#define IMX219_RHS1 (0x3060) // Readout timing setting of SEF1 Designated in line units.
#define IMX219_RHS2 (0x3064) // Readout timing setting of SEF2 Designated in line units.

#define GAIN_PCG0   (0x3090)
#define GAIN_PCG1   (0x3092)
#define GAIN_PCG2   (0x3094)
#define GAIN_PCG3   (0x3094)


#define IMX219_VAMX (0x3024)
#define IMX219_HAMX (0x3028)

#define PIX_HST (0x3040)
#define PIX_VST (0x3044)

#define PIX_HWIDTH (0x3042) //  PIX_HWIDTH
#define PIX_VWIDTH (0x3046) //  PIX_VWIDTH

#define IMX219_PARAM_HOLD  	(0x3001)
#define IMX219_EXP_REG_ADDR_HI 0x015A
#define IMX219_EXP_REG_ADDR_LO 0x015B
#define IMX219_GAIN 0x0157
#define IMX219_DGAIN 0x0158


#define IMX219_CSI_LANE_MODE (0x4001)  // 1:CSI-2 2lane, 3:CSI-2 4lane

// #define IMX219_VTS	       	(0x0340)
// #define IMX219_HTS	        (0x0342)
// #define IMX219_X_SIZE   	(0x034C)
// #define IMX219_Y_SIZE  		(0x034E)
// #define DEFAULT_FPS_REGVALUE	(3064)
// #define DEFAULT_FPS				(30)


#define IMX219_REG_CTRL_MODE 0x3000
#define IMX219_MODE_SW_STANDBY 0x1
#define IMX219_MODE_STREAMING 0x0

#define IMX219_REG_MASTER_MODE 0x3002
#define IMX219_MASTER_MODE_START 0x0
#define IMX219_MASTER_MODE_STOP 0x1


static uint32_t imx219_stream_on_setting[] = {
        0x0100, 0x1,
};

static uint32_t imx219_stream_off_setting[] = {
        0x0100, 0x0,
};

static uint32_t imx219_gain_lut[] = {
	0x00,
	0x05,
	0x0B,
	0x0F,
	0x15,
	0x1A,
	0x1F,
	0x24,
	0x28,
	0x2D,
	0x32,
	0x35,
	0x3A,
	0x3F,
	0x43,
	0x47,
	0x4B,
	0x4E,
	0x52,
	0x56,
	0x5A,
	0x5E,
	0x61,
	0x64,
	0x68,
	0x6B,
	0x6E,
	0x71,
	0x74,
	0x77,
	0x7A,
	0x7D,
	0x80,
	0x83,
	0x86,
	0x89,
	0x8B,
	0x8D,
	0x90,
	0x92,
	0x94,
	0x97,
	0x99,
	0x9B,
	0x9D,
	0x9F,
	0xA2,
	0xA4,
	0xA6,
	0xA7,
	0xA9,
	0xAB,
	0xAD,
	0xAF,
	0xB1,
	0xB2,
	0xB4,
	0xB6,
	0xB7,
	0xB9,
	0xBA,
	0xBC,
	0xBD,
	0xBF,
	0xC0,
	0xC1,
	0xC3,
	0xC4,
	0xC5,
	0xC6,
	0xC8,
	0xC9,
	0xCA,
	0xCB,
	0xCD,
	0xCE,
	0xCF,
	0xD0,
	0xD0,
	0xD2,
	0xD3,
	0xD4,
	0xD5,
	0xD6,
	0xD7,
	0xD7,
	0xD8,
	0xD9,
	0xDA,
	0xDB,
	0xDC,
	0xDD,
	0xDD,
	0xDE,
	0xDF,
	0xDF,
	0xE0,
	0xE1,
	0xE1,
	0xE2,
	0xE3,
	0xE4,
	0xE4,
	0xE5,
	0xE5,
	0xE6,
	0xE6,
	0xE7,
	0xE7,
	0xE8,
};

static uint32_t imx219_init_1920x1080_linear_setting[] = {
	0x0100, 0x00,
	0x30eb, 0x05,
	0x30eb, 0x0c,
	0x300a, 0xff,
	0x300b, 0xff,
	0x30eb, 0x05,
	0x30eb, 0x09,
	0x0114, 0x01,
	0x0128, 0x00,
	0x012a, 0x18,
	0x012b, 0x00,
	0x0160, 0x06, // frame lenth_h
	0x0161, 0xCD, // frame lenth_l
	0x0162, 0x0D, // line lenth_h
	0x0163, 0x78, // line lenth_l
	0x0164, 0x02,
	0x0165, 0xa8,
	0x0166, 0x0a,
	0x0167, 0x27,
	0x0168, 0x02,
	0x0169, 0xb4,
	0x016a, 0x06,
	0x016b, 0xeb,
	0x016c, 0x07,
	0x016d, 0x80,
	0x016e, 0x04,
	0x016f, 0x38,
	0x0170, 0x01,
	0x0171, 0x01,
	0x0174, 0x00,
	0x0175, 0x00,
	0x0301, 0x05,
	0x0303, 0x01,
	0x0304, 0x03,
	0x0305, 0x03,
	0x0306, 0x00,
	0x0307, 0x39,
	0x030b, 0x01,
	0x030c, 0x00,
	0x030d, 0x72,
	0x0624, 0x07,
	0x0625, 0x80,
	0x0626, 0x04,
	0x0627, 0x38,
	0x455e, 0x00,
	0x471e, 0x4b,
	0x4767, 0x0f,
	0x4750, 0x14,
	0x4540, 0x00,
	0x47b4, 0x14,
	0x4713, 0x30,
	0x478b, 0x10,
	0x478f, 0x10,
	0x4793, 0x10,
	0x4797, 0x0e,
	0x479b, 0x0e,
};

static uint32_t imx219_init_3264x2464_linear_setting_15fps[] = {
	0x0100, 0x00,
	0x30EB, 0x0C,
	0x30EB, 0x05,
	0x300A, 0xFF,
	0x300B, 0xFF,
	0x30EB, 0x05,
	0x30EB, 0x09,
	0x0114, 0x01,//CSI_LANE_MODE
	0x0128, 0x00,
	0x012A, 0x18,
	0x012B, 0x00,
	0x0160, 0x09,//IMX219_REG_VTS
	0x0161, 0xC8,//IMX219_REG_VTS
	0x0162, 0x0D,
	0x0163, 0x78,
	0x0164, 0x00,
	0x0165, 0x00,
	0x0166, 0x0C,
	0x0167, 0xCF,
	0x0168, 0x00,
	0x0169, 0x00,
	0x016A, 0x09,
	0x016B, 0x9F,
	0x016C, 0x0C,
	0x016D, 0xC0,//3264
	0x016E, 0x09,
	0x016F, 0xA0,//2464
	0x0170, 0x01,
	0x0171, 0x01,
	0x0174, 0x00,//IMX219_BINNING_NONE
	0x0175, 0x00,//IMX219_BINNING_NONE
	0x018C, 0x0A,//IMX219_REG_CSI_DATA_FORMAT_A raw10 :0x0a
	0x018D, 0x0A,//IMX219_REG_CSI_DATA_FORMAT_A raw10 :0x0a
	0x0301, 0x05,
	0x0303, 0x01,
	0x0304, 0x03,
	0x0305, 0x03,
	0x0306, 0x00,
	0x0307, 0x2B,
	0x0309, 0x0A,
	0x030B, 0x01,
	0x030C, 0x00,
	0x030D, 0x56,
	0x455E, 0x00,
	0x471E, 0x4B,
	0x4767, 0x0F,
	0x4750, 0x14,
	0x4540, 0x00,
	0x47B4, 0x14,
	0x4713, 0x30,
	0x478B, 0x10,
	0x478F, 0x10,
	0x4793, 0x10,
	0x4797, 0x0E,
	0x479B, 0x0E,
};

static uint32_t imx219_init_3264x2464_linear_setting_21fps[] = {
	0x0100, 0x00,
	/* sensor config */
	0x0114, 0x01, /* D-Phy, 2-lane */
	0x0128, 0x00,
	0x012A, 0x18, /* 24 MHz INCK */
	0x012B, 0x00,
	/* access code - vendor addr. ranges */
	0x30EB, 0x05,
	0x30EB, 0x0C,
	0x300A, 0xFF,
	0x300B, 0xFF,
	0x30EB, 0x05,
	0x30EB, 0x09,
	/* cis tuning */
	0x455E, 0x00,
	0x471E, 0x4B,
	0x4767, 0x0F,
	0x4750, 0x14,
	0x4540, 0x00,
	0x47B4, 0x14,
	0x4713, 0x30,
	0x478B, 0x10,
	0x478F, 0x10,
	0x4793, 0x10,
	0x4797, 0x0E,
	0x479B, 0x0E,
	/* capture settings */
	0x0157, 0x00, /* ANALOG_GAIN_GLOBAL[7:0] */
	0x015A, 0x09, /* COARSE_INTEG_TIME[15:8] */
	0x015B, 0xbd, /* COARSE_INTEG_TIME[7:0] */
	/* format settings */
	0x0160, 0x09, /* FRM_LENGTH[15:8] */
	0x0161, 0xC1, /* FRM_LENGTH[7:0] */
	0x0162, 0x0D, /* LINE_LENGTH[15:8] */
	0x0163, 0x78, /* LINE_LENGTH[7:0] */
	0x0164, 0x00,
	0x0165, 0x08,
	0x0166, 0x0C,
	0x0167, 0xC7,
	0x0168, 0x00,
	0x0169, 0x00,
	0x016A, 0x09,
	0x016B, 0x9F,
	0x016C, 0x0C,
	0x016D, 0xC0,
	0x016E, 0x09,
	0x016F, 0xA0,
	0x0170, 0x01,
	0x0171, 0x01,
	0x0174, 0x00,
	0x0175, 0x00,
	0x018C, 0x0A,
	0x018D, 0x0A,
	0x0264, 0x00,
	0x0265, 0x08,
	0x0266, 0x0C,
	0x0267, 0xC7,
	0x026C, 0x0C,
	0x026D, 0xC0,
	/* clock dividers */
	0x0301, 0x05,
	0x0303, 0x01,
	0x0304, 0x03,
	0x0305, 0x03,
	0x0306, 0x00,
	0x0307, 0x39,
	0x0309, 0x0A,
	0x030B, 0x01,
	0x030C, 0x00,
	0x030D, 0x72,
};

static uint32_t imx219_1632x1232_linear_setting_30fps[] = {
	0x30EB, 0x05,
	0x30EB, 0x0C,
	0x300A, 0xFF,
	0x300B, 0xFF,
	0x30EB, 0x05,
	0x30EB, 0x09,
	0x0114, 0x01,
	0x0128, 0x00,
	0x012A, 0x18,
	0x012B, 0x00,
	0x0160, 0x05,
	0x0161, 0x34,
	0x0162, 0x0D,
	0x0163, 0x78,
	0x0164, 0x00,
	0x0165, 0x00,
	0x0166, 0x0C,
	0x0167, 0xCF,
	0x0168, 0x00,
	0x0169, 0x00,
	0x016A, 0x09,
	0x016B, 0x9F,
	0x016C, 0x06,
	0x016D, 0x60,
	0x016E, 0x04,
	0x016F, 0xD0,
	0x0170, 0x01,
	0x0171, 0x01,
	0x0174, 0x01,
	0x0175, 0x01,
	0x018C, 0x0A,
	0x018D, 0x0A,
	0x0301, 0x05,
	0x0303, 0x01,
	0x0304, 0x03,
	0x0305, 0x03,
	0x0306, 0x00,
	0x0307, 0x2B,
	0x0309, 0x0A,
	0x030B, 0x01,
	0x030C, 0x00,
	0x030D, 0x56,
	0x455E, 0x00,
	0x471E, 0x4B,
	0x4767, 0x0F,
	0x4750, 0x14,
	0x4540, 0x00,
	0x47B4, 0x14,
	0x4713, 0x30,
	0x478B, 0x10,
	0x478F, 0x10,
	0x4793, 0x10,
	0x4797, 0x0E,
	0x479B, 0x0E,
};

static uint32_t imx219_640x480_linear_setting_30fps[] = {
	0x0103, 0x01,
	0x0100, 0x00,
	0x30EB, 0x05,
	0x30EB, 0x0C,
	0x300A, 0xFF,
	0x300B, 0xFF,
	0x30EB, 0x05,
	0x30EB, 0x09,
	0x0114, 0x01,
	0x0128, 0x00,
	0x012A, 0x18,
	0x012B, 0x00,
	0x0157, 0x00,
	0x0160, 0x06,
	0x0161, 0xA0,
	0x0162, 0x0D,
	0x0163, 0xE8,
	0x0164, 0x03,
	0x0165, 0xE8,
	0x0166, 0x08,
	0x0167, 0xE7,
	0x0168, 0x02,
	0x0169, 0xF0,
	0x016A, 0x06,
	0x016B, 0xAF,
	0x016C, 0x02,
	0x016D, 0x80,
	0x016E, 0x01,
	0x016F, 0xE0,
	0x0170, 0x01,
	0x0171, 0x01,
	0x0174, 0x03,
	0x0175, 0x03,
	0x018C, 0x0A,
	0x018D, 0x0A,
	0x0301, 0x05,
	0x0303, 0x01,
	0x0304, 0x03,
	0x0305, 0x03,
	0x0306, 0x00,
	0x0307, 0x39,
	0x0309, 0x0A,
	0x030B, 0x01,
	0x030C, 0x00,
	0x030D, 0x72,
	0x455E, 0x00,
	0x471E, 0x4B,
	0x4767, 0x0F,
	0x4750, 0x14,
	0x4540, 0x00,
	0x47B4, 0x14,
	0x4713, 0x30,
	0x478B, 0x10,
	0x478F, 0x10,
	0x4793, 0x10,
	0x4797, 0x0E,
	0x479B, 0x0E,
};
#ifdef __cplusplus
}
#endif

#endif  // UTILITY_SENSOR_INC_IMX219_SETTING_H_

